package com.msconfig.mvp.view.fragment;

/*
 * author: caojunhao
 * date  : 2020/5/11
 * desc  :
 */

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.geo.msconfig.R;
import com.msconfig.bean.dialogBean.OperatorBean;
import com.msconfig.event.EditOperateInfoEvent;
import com.msconfig.event.GprsAddOperatorSuccessEvent;
import com.msconfig.event.GprsOperatorManagerOnClickItemEvent;
import com.msconfig.mvp.view.activity.base.BaseMvpActivity;
import com.msconfig.mvp.view.activity.base.BaseMvpFragment;
import com.msconfig.mvp.view.activity.base.BasePresenter;
import com.msconfig.mvp.view.adapter.OperatorAdapter;
import com.msconfig.utils.DataCacheUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class SetupGprsOperatorManagerFragment extends BaseMvpFragment implements View.OnClickListener{
    private ImageView ivCommonBack;
    private TextView tvCommonTitle;
    private ListView lvOperator;
    private RelativeLayout rlAddOperators;
    private RelativeLayout rlChinaUnicom;
    private RelativeLayout rlChinaMobile;
    private RelativeLayout rlChinaTelecom;
    private OperatorAdapter adapter;
    private ArrayList<OperatorBean> list;
    private OperatorBean operatorBean;

    public static SetupGprsOperatorManagerFragment newInstance() {
        Bundle args = new Bundle();
        SetupGprsOperatorManagerFragment fragment = new SetupGprsOperatorManagerFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onClick(View v) {
        if(v==ivCommonBack){
            backTo();
        }else if(v==rlAddOperators){
            ((BaseMvpActivity)getActivity()).goToNextFragment(this, SetupGprsAddOperatorFragment.newInstance());
            //区别从编辑页进去
            EventBus.getDefault().postSticky(new EditOperateInfoEvent(false,-1,"","","",""));
        }else if(v==rlChinaUnicom){
            onClickRlChinaUnicom();
        }else if(v==rlChinaMobile){
            onClickRlChinaMobile();
        }else if(v==rlChinaTelecom){
            onClickRlChinaTelecom();
        }
    }

    private void onClickRlChinaTelecom() {
        operatorBean=new OperatorBean();
        String strName="";
        String strUser=getString(R.string.china_telecom_apn_user);
        String strPass=getString(R.string.china_telecom_apn_pass);
        operatorBean.setOperator(getString(R.string.china_telecom));
        operatorBean.setName(getString(R.string.operator_name, strName));
        operatorBean.setUser(getString(R.string.operator_user, strUser));
        operatorBean.setPass(getString(R.string.operator_pass, strPass));
        EventBus.getDefault().post(new GprsOperatorManagerOnClickItemEvent(operatorBean));
        backTo();
    }

    private void onClickRlChinaMobile() {
        operatorBean=new OperatorBean();
        String strName=getString(R.string.china_mobile_apn_name);
        String strUser="";
        String strPass="";
        operatorBean.setOperator(getString(R.string.china_mobile));
        operatorBean.setName(getString(R.string.operator_name, strName));
        operatorBean.setUser(getString(R.string.operator_user, strUser));
        operatorBean.setPass(getString(R.string.operator_pass, strPass));
        EventBus.getDefault().post(new GprsOperatorManagerOnClickItemEvent(operatorBean));
        backTo();
    }

    private void onClickRlChinaUnicom() {
        operatorBean=new OperatorBean();
        String strName=getString(R.string.china_unicom_apn_name);
        String strUser="";
        String strPass="";
        operatorBean.setOperator(getString(R.string.china_unicom));
        operatorBean.setName(getString(R.string.operator_name, strName));
        operatorBean.setUser(getString(R.string.operator_user, strUser));
        operatorBean.setPass(getString(R.string.operator_pass, strPass));
        EventBus.getDefault().post(new GprsOperatorManagerOnClickItemEvent(operatorBean));
        backTo();
    }

    @Override
    protected void createPresenter(List<BasePresenter> mPresenters) {

    }

    @Override
    protected int getLayoutId() {
        return R.layout.layout_fragment_setup_gprs_operator_manager;
    }

    @Override
    protected void initView(View view) {
        ivCommonBack=view.findViewById(R.id.iv_common_back);
        tvCommonTitle=view.findViewById(R.id.tv_common_title);
        lvOperator = (ListView)view.findViewById( R.id.lv_operator );
        rlAddOperators = (RelativeLayout)view.findViewById( R.id.rl_add_operators );
        rlChinaUnicom = (RelativeLayout)view.findViewById( R.id.rl_china_unicom );
        rlChinaMobile = (RelativeLayout)view.findViewById( R.id.rl_china_mobile );
        rlChinaTelecom = (RelativeLayout)view.findViewById( R.id.rl_china_telecom );
    }

    @Override
    protected void initListener(View view) {
        ivCommonBack.setOnClickListener(this);
        rlAddOperators.setOnClickListener(this);
        rlChinaUnicom.setOnClickListener(this);
        rlChinaMobile.setOnClickListener(this);
        rlChinaTelecom.setOnClickListener(this);

    }

    @Override
    protected void initData() {
        SetupGprsAddOperatorFragment fragment=SetupGprsAddOperatorFragment.newInstance();
        tvCommonTitle.setText(R.string.title_operator_manager);
        list=new ArrayList<>();
        adapter=new OperatorAdapter(getActivity(),this,list);
        addOperatorData();
        lvOperator.setAdapter(adapter);
        adapter.setOnContentClick(new OperatorAdapter.onSlideViewContentClickListener(){
            @Override
            public void onContentClick(int position) {
                OperatorBean operatorBean = list.get(position);
                EventBus.getDefault().post(new GprsOperatorManagerOnClickItemEvent(operatorBean));
                backTo();
            }
        });
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void showAddOperator(GprsAddOperatorSuccessEvent event) {
        OperatorBean operatorBean=event.getOperatorBean();
        if(event.getPosition()!=-1){
            //说明是编辑的，不是新添加的
            int position=event.getPosition();
            list.get(position).setOperator(operatorBean.getOperator());
            list.get(position).setName(operatorBean.getName());
            list.get(position).setUser(operatorBean.getUser());
            list.get(position).setPass(operatorBean.getPass());
        }else{
            list.add(operatorBean);
        }
        //保存当前运营商到文件中方便下次直接加载
        DataCacheUtils.saveListCache(getContext(),list,"already_add_operator");
        adapter.notifyDataSetChanged();
    }

    //固定的三个运营商
    private void addOperatorData() {

        //获取文件保存的已经添加的运营商信息
        ArrayList<OperatorBean> arrayList=DataCacheUtils.loadListCache(getContext(),"already_add_operator");
        Iterator<OperatorBean> iterator=arrayList.iterator();
        while(iterator.hasNext()){
            OperatorBean bean=iterator.next();
            list.add(bean);
        }
        adapter.notifyDataSetChanged();
    }
}
